//
//  BLStatusToolBar.m
//  新浪微博
//
//  Created by hbl on 16/2/25.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLStatusToolBar.h"
#import "BLStatus.h"
#import "Access.h"
#import "AccessTool.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"
#define toolBarFont [UIFont systemFontOfSize:13]
@interface BLStatusToolBar()
@property (nonatomic, strong)NSMutableArray *toolBarArray;
@property (nonatomic, strong)NSMutableArray *toolBarDiverArray;
@property (nonatomic, weak)UIButton *leftBtn;
@property (nonatomic, weak)UIButton *middleBtn;
@property (nonatomic, weak)UIButton *rightBtn;


@end

@implementation BLStatusToolBar

- (NSMutableArray *)toolBarArray
{
    if (!_toolBarArray) {
        _toolBarArray = [NSMutableArray array];
    }
    return _toolBarArray;
}
- (NSMutableArray *)toolBarDiverArray
{
    if (!_toolBarDiverArray) {
        _toolBarDiverArray = [NSMutableArray array];
    }
    return _toolBarDiverArray;
}

- (void)setStatus:(BLStatus *)status
{
    _status = status;
    if (status.reposts_count) {
        [self.leftBtn setTitle:[NSString stringWithFormat:@"%d",status.reposts_count] forState:UIControlStateNormal];
    }
    
    if (status.comments_count) {
        [self.middleBtn setTitle:[NSString stringWithFormat:@"%d",status.comments_count] forState:UIControlStateNormal];
    }
    
    if (status.attitudes_count) {
        [self.rightBtn setTitle:[NSString stringWithFormat:@"%d",status.attitudes_count] forState:UIControlStateNormal];
        [self.rightBtn addTarget:self action:@selector(onClicZhuan:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}

- (void)onClicZhuan:(id)btn
{
    
//    Access *access = [AccessTool getTheAccess];
//    NSString *token = access.access_token;
//    AFHTTPSessionManager *menager = [[AFHTTPSessionManager alloc]init];
//    NSMutableDictionary *parameters = [[NSMutableDictionary alloc]init];
//    parameters[@"attitude"] = @"simle";
//    parameters[@"access_token"] = token;
//    parameters[@"id"] = self.status.idstr;
//    
//    [menager POST:@"https://api.weibo.com/2/attitudes/create.json" parameters:parameters constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"seccess");
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"fail-%@",error);
//    }];
    [MBProgressHUD showError:@"新浪未开放！"];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        UIImage *toolBarViewImag = [UIImage imageNamed:@"common_card_bottom_background_os7"];
        toolBarViewImag = [toolBarViewImag resizableImageWithCapInsets:UIEdgeInsetsMake(toolBarViewImag.size.height*0.5, toolBarViewImag.size.width*0.5, toolBarViewImag.size.height*0.5, toolBarViewImag.size.width*0.5)];
        self.image = toolBarViewImag;
        
        //添加按钮
        UIButton *leftBtn = [self setUpToolButtonWithTitle:@"转发" imgName:@"timeline_icon_retweet_os7" heighnightBackground:@"timeline_card_leftbottom_highlighted_os7"];
        [self addSubview:leftBtn];
        self.leftBtn = leftBtn;
        
        
        UIButton *middleBtn = [self setUpToolButtonWithTitle:@"评论" imgName:@"timeline_icon_comment_os7" heighnightBackground:@"timeline_card_middlebottom_highlighted_os7"];
        [self addSubview:middleBtn];
        self.middleBtn = middleBtn;
        
        UIButton *rightBtn = [self setUpToolButtonWithTitle:@"赞" imgName:@"timeline_icon_unlike_os7" heighnightBackground:@"timeline_card_rightbottom_highlighted_os7"];
        [self addSubview:rightBtn];
        self.rightBtn = rightBtn;
        
        //分割线
        for (int i = 0; i<2; i++) {
            UIImageView *diver = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"timeline_card_bottom_line_os7"]];
            [self.toolBarDiverArray addObject:diver];
            [self addSubview:diver];
        }

    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat btnY = 0;
    CGFloat btnH = self.bounds.size.height;
    CGFloat btnW = (self.bounds.size.width - 4)/self.toolBarArray.count;
    for (int i = 0; i< self.toolBarArray.count; i++) {
        UIButton *btn = self.toolBarArray[i];
        CGFloat btnX = i * (btnW + 2);
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
    
    
    CGFloat imgViewY = 0;
    CGFloat imgViewW = 2;
    CGFloat imgViewH = btnH;
    for (int i = 0; i < self.toolBarDiverArray.count; i++) {
        UIImageView *imgView = self.toolBarDiverArray[i];
        UIButton *toolButton = self.toolBarArray[i];
        CGFloat imgViewX = CGRectGetMaxX(toolButton.frame);
        imgView.frame = CGRectMake(imgViewX, imgViewY, imgViewW, imgViewH);
    }
}


- (UIButton *)setUpToolButtonWithTitle:(NSString *)title imgName:(NSString *)imgName heighnightBackground:(NSString *)hBackground
{
    UIButton *btn = [[UIButton alloc]init];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [btn setBackgroundImage:[self resizeMyImg:hBackground] forState:UIControlStateHighlighted];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btn.titleLabel.font = toolBarFont;
    [self.toolBarArray addObject:btn];
    return btn;
}

- (UIImage *)resizeMyImg:(NSString *)imgNamme
{
    UIImage *img = [UIImage imageNamed:imgNamme];
    img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(img.size.height * 0.5, img.size.width * 0.5, img.size.height * 0.5, img.size.width * 0.5)];
    return img;
}
@end
